package com.example.start

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.TextView
import java.security.AccessController.getContext


/* 把数据组装到页面控件中，生成所有的行 */
class PolicyItemAdapter(productions: List<PolicyItem>, context: Context) : BaseAdapter() {
    // 数据bean
    private val policyItems // 商品集合
            : List<PolicyItem>
    private val context // 上下文对象
            : Context

    /**
     * 含参构造
     * @param policyItems  商品集合
     * @param context   上下文对象
     */
    init {
        this.policyItems = productions
        this.context = context
    }

    /**
     * @return 获取总加载数量，返回总数据集合的大小
     */
    override fun getCount(): Int {
        return policyItems.size
    }

    /**
     * @param position  获取具体行的控件item
     * @return  返回获取具体行的控件item
     */
    override fun getItem(position: Int): Any {
        return policyItems[position]
    }

    /**
     * @param position  当前位置的行号
     * @return 返回当前位置的行号
     */
    override fun getItemId(position: Int): Long {
        return position.toLong()
    }
    override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {

        // Get the data item for this position
        var convertView = convertView
        val policyItem: PolicyItem = getItem(position) as PolicyItem

        // Check if an existing view is being reused, otherwise inflate the view
        if (convertView == null) {
            convertView =
                LayoutInflater.from(context).inflate(R.layout.policy_item_layout, parent, false)
        }

        // Lookup view for data population
        val tvName = convertView!!.findViewById<View>(R.id.tvTitle) as TextView
        val tvHome = convertView.findViewById<View>(R.id.tvUrl) as TextView

        // Populate the data into the template view using the data object
        tvName.setText(policyItem.title)
        tvHome.setText(policyItem.url)

        // Return the completed view to render on screen
        return convertView
    }
}